{% extends "home_base.html" %}
{% load seahub_tags i18n %}

{% block sub_title %}{% trans "Libraries" %} - {% endblock %}

{% block extra_style %}
{% if need_guide %}
<style type="text/css">
#guide-for-new {
    padding: 0 20px;
    width: 450px;
}
#guide-for-new .icon-lightbulb {
    font-size:6em;
    color:#feac74;
}
#guide-for-new .txt {
    margin-left:6.4em;
}
</style>
{% endif %}
{% endblock %}

{% block cur_my_lib %}tab-cur{% endblock %}

{% block right_panel %}
{% include "snippets/my_owned_repos.html" %}

{% url 'share_repo' as repo_share_url %}
{% with post_url=repo_share_url %}
{% include "snippets/repo_share_form.html" %}
{% endwith %}

{% include "snippets/repo_create_form.html" %}

{% if need_guide %}
<div id="guide-for-new" class="hide">
    <span class="icon-lightbulb fleft"></span>
    <div class="txt">
        <h3>{% trans "Welcome to Seafile!" %}</h3>
        {% if user.permissions.can_add_repo %}
        <p>{% trans "Seafile organizes files into libraries. Each library can be synced and shared separately. We have created a personal library for you. You can create more libraries later." %}</p>
        {% else %}
        <p>{% trans "Seafile organizes files into libraries. Each library can be synced and shared separately. Howerver, since you are a guest user now, you can not create libraries." %}</p>
        {% endif %}
        <button class="simplemodal-close" style="margin:8px 0 0 0;">{% trans "Close" %}</button>
    </div>
</div>
{% endif %}

{% if ENABLE_SUB_LIBRARY and sub_lib_enabled %}
<form id="sublib-create-form" class="file-choose-form hide">
    <h3>{% trans "Choose a directory:" %}</h3>
    <div class="dir-tree-cont">
        <img src="{{MEDIA_URL}}img/loading-icon.gif" alt="" class="loading-tip" />
    </div>
    <input type="hidden" name="dst_repo" value="" />
    <input type="hidden" name="dst_path" value="" />
    <p class="error hide"></p>
    <input type="button" value="{% trans "Submit" %}" class="submit" />
    <button class="simplemodal-close">{% trans "Cancel"%}</button>
</form>
{% endif %}

{% endblock %}

{% block extra_script %}{{block.super}}
<script type="text/javascript">
{% if need_guide %}
$(function() {$('#guide-for-new').modal({appendTo: '#main', focus:false});});
{% endif %}
{% include "snippets/myhome_extra_script.html" %}
var cur_tab = $('.ui-tabs-selected .a');    
var lib_create_btn = $('#repo-create'),
    sublib_create_btn = $('#sub-lib-create');
// for 'long language' such as german
if (sublib_create_btn.text().length > 15) { // 'new Sub-library'.length is 15
    $('span', sublib_create_btn).text(trimFilename(sublib_create_btn.text(),14));;
}

// for initial state
if (cur_tab.attr('id') == 'mylib-tab') {
    lib_create_btn.removeClass('hide');
}
if (cur_tab.attr('id') == 'sublib-tab') {
    sublib_create_btn.removeClass('hide');
}
$('#mylib-tab').click(function() {
    lib_create_btn.removeClass('hide');
    sublib_create_btn.addClass('hide');
});
$('#sublib-tab').click(function() {
    lib_create_btn.addClass('hide');
    sublib_create_btn.removeClass('hide');
});
$('#shared-lib-tab, #grp-lib-tab').click(function() {
    lib_create_btn.addClass('hide');
    sublib_create_btn.addClass('hide');
});
{% if ENABLE_SUB_LIBRARY and sub_lib_enabled %}
var sublib_create_form = $('#sublib-create-form');
sublib_create_btn.click(function() {
    var dir_tree_cont = $('.dir-tree-cont', sublib_create_form);
    sublib_create_form.modal();
    $.ajax({
        url: '{% url 'get_my_unenc_repos' %}',
        cache: false,
        dataType: 'json',
        success: function(data) {
            var file_tree = new FileTree();
            var repos = file_tree.format_repo_data(data);
            if (repos.length > 0) {
                file_tree.renderDirTree(dir_tree_cont.data('site_root', '{{SITE_ROOT}}'), sublib_create_form, repos);
            } else {
                dir_tree_cont.html('<p class="error">' + "{% trans "You don't have any library at present" %}" + '</p>');
            }   
        },  
        error: function(jqXHR, textStatus, errorThrown) {
            var error;
            if (jqXHR.responseText) {
                error = $.parseJSON(jqXHR.responseText).error;
            } else {
                error = "{% trans "Failed. Please check the network." %}";
            }
            dir_tree_cont.html('<p class="error">' + error + '</p>');
        }   
    }); 
});
$('.submit', sublib_create_form).click(function() {
    var ori_repo_id = $('[name="dst_repo"]', sublib_create_form).val();    
    var path = $('[name="dst_path"]', sublib_create_form).val();    
    
    if (!path || path == '/') {
        $('.error', sublib_create_form).html("{% trans "Please choose a directory" %}").removeClass('hide');
        return false;
    } 
    
    // path ends with '/', rm it here 
    path = path.substr(0, path.length - 1); 
    $.ajax({
        url: '{{SITE_ROOT}}ajax/repo/' + ori_repo_id + '/dir/sub_repo/?p=' + e(path),
        dataType: 'json',
        success: function(data) {
            location.reload();
        },
        error: function(xhr, textStatus, errorThrown) {
            var err;
            if (xhr.responseText) {
                err = jQuery.parseJSON(xhr.responseText).error;
            } else {
                err = "{% trans "Failed. Please check the network." %}";
            }
            $('.error', sublib_create_form).html(err).removeClass('hide');
        }
    });
    return false;
});
{% endif %}
{% include "snippets/repo_create_js.html" with post_url=repo_create_url %}
</script>
{% endblock %}
